package com.rs2hd.skills.woodcutting;

import com.rs2hd.event.Event;
import com.rs2hd.model.Player;
import com.rs2hd.model.Skills;
import com.rs2hd.model.World;

public class Woodcutting {
	private Player p;
	private int anim;
	private int speed;
	private int time;
	private int objid;
	private int treeid;
	private int logTimer = -1;
	private int secondtimer = 2;
	private int logs = 0;
	private int maxlogs = 0;
	private boolean chopping = false;
	public Woodcutting(Player p2) {
		p = p2;
	}
	public void startCutting(int objectid) {
		if (getPlayerAxe() == -1) {
			p.getActionSender().sendMessage("You don't have an axe wich you have the woodcutting level to use.");
			return;
		}
		objid = objectid;
		treeid = getTreeIDForObject(objid);
		if (getLevelForTree(treeid) > getWoodcuttingLevel()) {
			p.getActionSender().sendMessage("You need level " + getLevelForTree(treeid)+" to chop down this tree");
			return;
		}
		p.getActionSender().sendMessage("You swing your axe at the tree");
		maxlogs = getMaxAmountOfLogs(treeid);
		if (maxlogs == 0) maxlogs = 1;
		setAnimAndSpeed(treeid, getPlayerAxe(), getWoodcuttingLevel());
		logTimer = time;
		p.animate(anim);
		if (!chopping) {
			process();
		}
		chopping = true;
	}
	public void process() {
		World.getInstance().registerEvent(new Event(500) {
			@Override
			public void execute() {
				if (p == null) {
					this.stop();
				}
				if (!chopping) {
					this.stop();
					return;
				}
				if (chopping) {
					secondtimer--;
					if (secondtimer == 0) {
						secondtimer = 2;
						logTimer--;
						p.animate(anim);
					}
					if (logTimer == 0) {
						logTimer = time;
						logs++;
						doLog();
					}
				}
			}
		});
	}
	public void resetWoodcutting() {
		logTimer = -1;
		chopping = false;
	}
	public void doLog() {
		p.getActionSender().sendMessage("You cut some logs.");
		giveItem(getLogsForTree(treeid));
		giveWoodcuttingXP(getXpForLog(treeid));
		p.animate(anim);
	}
	public int getWoodcuttingLevel() {
		return p.getSkills().getLevel(Skills.WOODCUTTING);
	}
	public void giveWoodcuttingXP(int xp) {
		p.getSkills().addXp(Skills.WOODCUTTING , xp);
	}
	public boolean hasItem(int id) {
		if (p.getInventory().numberOf(id) == 0) {
			return false;
		}
		return true;
	}
	public void giveItem(int id) {
		p.getInventory().addItem(id, 1);
	}
	public int getPlayerAxe() {
		if (hasItem(1351)) return 1351;
		if (hasItem(1349)) return 1349;
		if (hasItem(1353)) return 1353;
		if (hasItem(1361)) return 1361;
		if (hasItem(1355)) return 1355;
		if (hasItem(1357)) return 1357;
		if (hasItem(1359)) return 1359;
		if (hasItem(6739)) return 6739;
		if (p.getEquipment().get(3) != null) return -1;
		if (p.getEquipment().get(3).getDefinition().getId() == 1351) return 1351;
		if (p.getEquipment().get(3).getDefinition().getId() == 1349) return 1349;
		if (p.getEquipment().get(3).getDefinition().getId() == 1353) return 1353;
		if (p.getEquipment().get(3).getDefinition().getId() == 1361) return 1361;
		if (p.getEquipment().get(3).getDefinition().getId() == 1355) return 1355;
		if (p.getEquipment().get(3).getDefinition().getId() == 1357) return 1357;
		if (p.getEquipment().get(3).getDefinition().getId() == 1359) return 1359;
		if (p.getEquipment().get(3).getDefinition().getId() == 6739) return 6739;
		return -1;		
	}
	public int getTreeIDForObject(int tree) {
		switch (tree) {
		case 1276:
		case 1277:
		case 1278:
		case 1279:
		case 1280:
		case 1282:
		case 1283:
		case 1284:
		case 1285:
		case 1286:
		case 1289:
		case 1290: 
		case 1291:
		case 1315:
		case 1316:
		case 1318:
		case 1319:
		case 1330:		
		case 1331:
		case 1332: 
		case 1365:
		case 1383:  
		case 1384:
		case 2409: 
		case 3033:
		case 3034:
		case 3035: 
		case 3036:	
		case 3881:
		case 3882:		
		case 3883:  
		case 5902:
		case 5903:  
		case 5904://Normal Trees:Tree,Dead tree, Evergreen
			return 1;

		case 1281:
		case 3037://Oak
			return 2;

		case 1308:
		case 5551:
		case 5552:
		case 5553://Willow
			return 3;

		case 9036://Teak
			return 4;

		case 1307://Maple
			return 5;

		case 9034://Mahogany
			return 6;
		case 1309://Yew
			return 7;
		case 1292: 
		case 1306://Magic
			return 8;
		}
		return -1;
	}
	public int getLogsForTree(int tree) {
		switch (tree) {
		case 1://Normal Trees:Tree,Dead tree, Evergreen
			return 1511;

		case 2://Oak
			return 1521;

		case 3://Willow
			return 1519;

		case 4://Teak
			return 6333;

		case 5://Maple
			return 1517;

		case 6://Mahogany
			return 6332;

		case 7://Yew
			return 1515;

		case 8://Magic
			return 1513;

		}
		return -1;
	}
	public int getMaxAmountOfLogs(int tree) {
		switch (tree) {
		case 1://Normal Trees:Tree,Dead tree, Evergreen
			return 1;

		case 2://Oak
			return 2;

		case 3://Willow
			return 3;

		case 4://Teak
			return 4;

		case 5://Maple
			return 5;

		case 6://Mahogany
			return 5;

		case 7://Yew
			return 4;

		case 8://Magic
			return 8;

		}
		return -1;
	}
	public int getXpForLog(int tree) {
		switch (tree) {
		case 1://Normal Trees:Tree,Dead tree, Evergreen
			return 50;

		case 2://Oak
			return 75;

		case 3://Willow
			return 100;

		case 4://Teak
			return 150;

		case 5://Maple
			return 175;

		case 6://Mahogany
			return 250;

		case 7://Yew
			return 300;

		case 8://Magic
			return 500;

		}
		return -1;
	}
	public int getLevelForTree(int tree) {
		switch (tree) {
		case 1://Normal Trees:Tree,Dead tree, Evergreen
			return 1;

		case 2://Oak
			return 15;

		case 3://Willow
			return 30;

		case 4://Teak
			return 35;

		case 5://Maple
			return 45;

		case 6://Mahogany
			return 50;

		case 7://Yew
			return 60;

		case 8://Magic
			return 75;

		}
		return -1;
	}
	public int getLevelForAxe(int axe) {
		switch (axe) {
		case 1351://Bronze Axe
			return 0;

		case 1349://Iron Axe
			return 0;

		case 1353://Steel Axe
			return 5;

		case 1361://Black Axe
			return 10;

		case 1355://Mithril Axe
			return 21;

		case 1357://Adamant Axe
			return 31;

		case 1359://Rune Axe
			return 41;

		case 6739://Dragon Axe
			return 61;

		}
		return -1;
	}
	public void setAnimAndSpeed(int tree,int axe,int level) {
		switch(axe) {
		case 1351://Bronze Axe
			anim = 879;
			speed = 1;
			break;
		case 1349://Iron Axe
			anim = 877;
			speed = 2;
			break;
		case 1353://Steel Axe
			anim = 875;
			speed = 5;
			break;
		case 1361://Black Axe
			anim = 873;
			speed = 7;
			break;
		case 1355://Mithril Axe
			anim = 871;
			speed = 13;
			break;
		case 1357://Adamant Axe
			anim = 869;
			speed = 20;
			break;
		case 1359://Rune Axe
			anim = 867;
			speed = 100;
			break;
		case 6739://Dragon Axe
			anim = 2846;
			speed = 35;
			break;
		}
		speed = (speed - (speed * 2)) / 10;
		time = 4;
		if (time < 0) time = 1;
		if (time == 1) anim++;
	}
}
